/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.easoft.code.pos.ui.beans;

import com.easoft.code.pos.ui.controllers.CargaMasivaController;
import com.easoft.code.pos.ui.controllers.CargaResumen;
import com.easoft.code.pos.ui.controllers.exception.ControllerException;
import com.easoft.code.pos.util.UIUtil;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
 *
 * @author Eliu
 */
public class FormCargaMasiva extends javax.swing.JPanel implements FormInvOperacionesMod {

    private File selFile;

    /**
     * Creates new form FormCargaMasiva
     */
    public FormCargaMasiva() {
        initComponents();
    }

    @Override
    public String getIconName() {
        return "cargamasiva.png";
    }

    @Override
    public String getOptionName() {
        return "Carga Masiva";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btnProcessFile = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtFileName = new javax.swing.JTextField();
        btnFileCh = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtCantFilas = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        lblAvance = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setPreferredSize(new java.awt.Dimension(400, 30));

        jLabel1.setText("Carga Masiva de Productos");
        jPanel1.add(jLabel1);

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel3.setPreferredSize(new java.awt.Dimension(400, 30));

        btnProcessFile.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/play-5.png"))); // NOI18N
        btnProcessFile.setText("Procesar");
        btnProcessFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProcessFileActionPerformed(evt);
            }
        });
        jPanel3.add(btnProcessFile);

        add(jPanel3, java.awt.BorderLayout.PAGE_END);

        jPanel4.setPreferredSize(new java.awt.Dimension(150, 70));
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setText("Archivo:");
        jPanel4.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 20, 60, -1));

        txtFileName.setEditable(false);
        jPanel4.add(txtFileName, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 20, 340, -1));

        btnFileCh.setText("...");
        btnFileCh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFileChActionPerformed(evt);
            }
        });
        jPanel4.add(btnFileCh, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 20, 40, -1));

        jLabel3.setText("Saltar Filas:");
        jPanel4.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 50, -1, -1));

        txtCantFilas.setText("1");
        txtCantFilas.setToolTipText("");
        jPanel4.add(txtCantFilas, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 50, 40, -1));

        jLabel4.setText("Avance:");
        jPanel4.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 80, -1, -1));

        lblAvance.setText("0%");
        jPanel4.add(lblAvance, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 80, -1, -1));

        add(jPanel4, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void btnFileChActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFileChActionPerformed
        final JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            selFile = fc.getSelectedFile();
            txtFileName.setText(selFile.getPath());
        } else {
            System.out.println("Open command cancelled by user.");
        }
    }//GEN-LAST:event_btnFileChActionPerformed

    private void btnProcessFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProcessFileActionPerformed
        final CargaMasivaController crl = new CargaMasivaController();
        CargaResumen cargaResumen;
        if (selFile.exists()) {
            cargaResumen = crl.getCargaResumen(selFile, Integer.valueOf(txtCantFilas.getText()));
            StringBuffer sb = new StringBuffer("El archivo contiene: ").append(cargaResumen.getTotalFilas());
            sb.append(". Desea continuar?");
            if (UIUtil.confirm(sb.toString(), "Carga Masiva")) {
                Thread t = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        CargaResumen cargaResumen;
                        try {
                            cargaResumen = crl.processCarga(FormCargaMasiva.this.lblAvance, Integer.valueOf(txtCantFilas.getText()));
                            UIUtil.showMsg("Filas almacenadas: " + cargaResumen.getTotalFilasOk(), "Carga Masiva");
                        } catch (ControllerException ex) {
                            Logger.getLogger(FormCargaMasiva.class.getName()).log(Level.SEVERE, null, ex);
                        }

                    }
                });
                t.start();
            }
        } else {
            UIUtil.showError("El archivo ya no existe!!", "Carga Masiva");
        }
    }//GEN-LAST:event_btnProcessFileActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFileCh;
    private javax.swing.JButton btnProcessFile;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    public javax.swing.JLabel lblAvance;
    private javax.swing.JTextField txtCantFilas;
    private javax.swing.JTextField txtFileName;
    // End of variables declaration//GEN-END:variables
}
